package com.vegetable.modules.finance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vegetable.common.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 银联商务网服-实时交易查询业务服务类
 * 实现网服相关接口调用
 */
@Slf4j
@Service
public class ChinaUMSWFService {

    // 测试环境基础URL
    private static final String TEST_BASE_URL = "https://test-api-open.chinaums.com";
    
    // 生产环境基础URL
    private static final String PROD_BASE_URL = "https://api-mop.chinaums.com";
    
    // 实时交易查询接口路径
    private static final String QUERY_TRANS_PATH = "/v1/uis/wf/query-trans";
    
    // 对账明细查询接口路径
    private static final String QUERY_SETTLE_DETAIL_PATH = "/v1/uis/wf/query-settle-detail";
    
    // 实时交易汇总查询接口路径
    private static final String QUERY_TRANS_SUMMARY_PATH = "/v1/uis/wf/query-trans-summary";
    
    // 对账汇总查询接口路径
    private static final String ACCOUNT_SUMMARY_PATH = "/v1/uis/wf/account-summary";

    /**
     * 实时交易查询接口
     * @param params 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    public Map<String, Object> queryTrans(Map<String, String> params, String authorization, boolean isProduction) {
        try {
            String url = (isProduction ? PROD_BASE_URL : TEST_BASE_URL) + QUERY_TRANS_PATH;
            
            // 构建请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", authorization);
            headers.put("Content-Type", "application/json");
            
            // 发送请求
            JSONObject response = HttpUtil.jsonPost(url, JSONObject.parseObject(JSON.toJSONString(params)));
            
            // 解析响应
            Map<String, Object> result = new HashMap<>();
            result.put("requestParams", params);
            result.put("response", response);
            return result;
        } catch (Exception e) {
            log.error("实时交易查询接口调用失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("error", e.getMessage());
            return errorResult;
        }
    }
    
    /**
     * 对账明细查询接口
     * @param params 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    public Map<String, Object> querySettleDetail(Map<String, String> params, String authorization, boolean isProduction) {
        try {
            String url = (isProduction ? PROD_BASE_URL : TEST_BASE_URL) + QUERY_SETTLE_DETAIL_PATH;
            
            // 构建请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", authorization);
            headers.put("Content-Type", "application/json");
            
            // 发送请求
            JSONObject response = HttpUtil.jsonPost(url, JSONObject.parseObject(JSON.toJSONString(params)));
            
            // 解析响应
             Map<String, Object> result = new HashMap<>();
            result.put("requestParams", params);
            result.put("response", response);
            return result;
        } catch (Exception e) {
            log.error("对账明细查询接口调用失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("error", e.getMessage());
            return errorResult;
        }
    }
    
    /**
     * 实时交易汇总查询接口
     * @param params 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    public Map<String, Object> queryTransSummary(Map<String, String> params, String authorization, boolean isProduction) {
        try {
            String url = (isProduction ? PROD_BASE_URL : TEST_BASE_URL) + QUERY_TRANS_SUMMARY_PATH;
            
            // 构建请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", authorization);
            headers.put("Content-Type", "application/json");
            
            // 发送请求
            JSONObject response = HttpUtil.jsonPost(url, JSONObject.parseObject(JSON.toJSONString(params)));
            
            // 解析响应
            Map<String, Object> result = new HashMap<>();
            result.put("requestParams", params);
            result.put("response", response);
            return result;
        } catch (Exception e) {
            log.error("实时交易汇总查询接口调用失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("error", e.getMessage());
            return errorResult;
        }
    }
    
    /**
     * 对账汇总查询接口
     * @param params 请求参数
     * @param authorization 认证信息
     * @param isProduction 是否生产环境
     * @return 响应结果
     */
    public Map<String, Object> accountSummary(Map<String, String> params, String authorization, boolean isProduction) {
        try {
            String url = (isProduction ? PROD_BASE_URL : TEST_BASE_URL) + ACCOUNT_SUMMARY_PATH;
            
            // 构建请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", authorization);
            headers.put("Content-Type", "application/json");
            
            // 发送请求
            JSONObject response = HttpUtil.jsonPost(url, JSONObject.parseObject(JSON.toJSONString(params)));
            
            // 解析响应
            Map<String, Object> result = new HashMap<>();
            result.put("requestParams", params);
            result.put("response", response);
            return result;
        } catch (Exception e) {
            log.error("对账汇总查询接口调用失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("error", e.getMessage());
            return errorResult;
        }
    }
}